$(function () {
    // 获取元素
    let $username = $("#username");
    let $password = $("#password");
    let $loginBtn = $("#loginBtn");
    let $remember = $("#remember");

    // 尝试从本地存储中获取数据 
    let username = localStorage.getItem("username");
    let password = localStorage.getItem("password");

    var isLogin = $.cookie("isLogin");
    if (isLogin == "1") {
        location.href = "./index.html";
    }
    if (username) {
        $username.val(username);
        $password.val(password);
        $remember.attr("checked", "checked");
    }

    // 记住我功能
    $loginBtn.on("click", function () {
        $.ajax({
            url: "/php/login.php",
            type: "post",
            dataType: "json",
            data: $("form").serialize(), 
            success(data) {
                if (!data.error) {
                    if ($remember.is(":checked")) {
                        localStorage.setItem("username", $username.val());
                        localStorage.setItem("password", $password.val())
                    } else {
                        localStorage.removeItem("username");
                        localStorage.removeItem("password")
                    }
                    location.href = '../index.html';
                } else {
                    $('#modal').modal('show'); 
                }
            }
        })

    })
})